package movie.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Cinema;
import movie.entity.Consumer;
import movie.entity.Myorder;
import movie.entity.Order;
import movie.service.CinemaScheduleFindAllService;
import movie.service.OrderService;

@Controller
public class OrderController {
	@Autowired
	OrderService orderService;
	@Autowired
	CinemaScheduleFindAllService cinemaScheduleFindAllService;
	@RequestMapping(value = "/myorders", method = RequestMethod.GET)
	public String get(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Consumer c1 = (Consumer)session.getAttribute("cur_user");
		List<Order> o = orderService.findOrder(c1.getUserId());
		List<Myorder> m = new ArrayList<>();
		for(Order i:o) {
			Myorder j = new Myorder();
			j.setOrderId(i.getOrderId());
			j.setAllPrice(i.getAllPrice());
			j.setOrderDate(i.getOrderDate());
			j.setSeatId(orderService.findSeat(i.getOrderId()));
			
			Cinema k = cinemaScheduleFindAllService.findByScheduleId(i.getScheduleId());
			
			j.setCover(orderService.findCover(k.getMovieId()));
			j.setMovieName(k.getFilm().getFilmName());
			j.setCinemaName(k.getCinemaName().getCinemaName());
			j.setHallName(k.getHall().getHallName());
			j.setMovieTime(k.getDate() + " " + k.getBeginHour() + ":" + k.getBeginMin()
			+ "至" + k.getEndHour() + ":" + k.getEndMin());
			m.add(j);
		}
		model.addAttribute("orders", m);
		return "/user/orders";
	}
}
